package com.rk.study02;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class FlowCountDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 输入文件
       // args = new String[]{"D:\\hadoop_study_data\\study02\\input\\phone_data.txt", "D:\\hadoop_study_data\\study02\\output"};
        args = new String[]{"hdfs://192.168.45.101:9000/data2/input", "hdfs://192.168.45.101:9000/data2/output"};
        // 1.获取配置文件信息
        Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");
        conf.set("mapreduce.job.jar", "D:\\projects\\hadoop-study\\target\\hadoop-study-1.0-SNAPSHOT.jar");
        conf.set("mapreduce.app-submission.cross-platform", "true");//意思是跨平台提交
        Job job = Job.getInstance(conf);

        // 2.设置jar
        job.setJarByClass(FlowCountDriver.class);

        // 3.设置map和reducer类
        job.setMapperClass(FlowCountMapper.class);
        job.setReducerClass(FlowCountReducer.class);

        // 4.设置mapper的输出数据的k、v类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);

        // 5.设置最终的输出的数据的k、v类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        // 6.设置输入文件所在路径和输出所在路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 7.将job所需的参数和所配置的类提交给YARN运行
        boolean result = job.waitForCompletion(true);
        System.exit(result? 0: 1);
    }
}
